package org.dromara.im.controller;

import cn.hutool.core.util.StrUtil;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.im.domain.Friend;
import org.dromara.im.domain.vo.FriendVO;
import org.dromara.im.service.IFriendService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/friend")
@RequiredArgsConstructor
public class FriendController {

    private final IFriendService friendService;

    @GetMapping("/list")
    public R<List<FriendVO>> findFriends() {
        List<Friend> friends = friendService.findFriendByUserId(LoginHelper.getUserId());
        List<FriendVO> vos = friends.stream().map(f -> {
            FriendVO vo = new FriendVO();
            vo.setId(f.getFriendId());
            vo.setHeadImage(f.getFriendHeadImage());
            vo.setNickName(f.getFriendNickName());
            return vo;
        }).collect(Collectors.toList());
        return R.ok(vos);
    }


    @PostMapping("/add")
    public R addFriend(@NotNull (message = "好友id不可为空") @RequestParam("friendId") Long friendId) {
        friendService.addFriend(friendId);
        return R.ok();
    }

    @GetMapping("/find/{friendId}")
    public R<FriendVO> findFriend(@NotEmpty(message = "好友id不可为空") @PathVariable("friendId") Long friendId) {
        return R.ok(friendService.findFriend(friendId));
    }


    @DeleteMapping("/delete/{friendId}")
    public R delFriend(@NotEmpty(message = "好友id不可为空") @PathVariable("friendId") Long friendId) {
        friendService.delFriend(friendId);
        return R.ok();
    }

    @PutMapping("/update")
    public R modifyFriend(@Valid @RequestBody FriendVO vo) {
        friendService.update(vo);
        return R.ok();
    }

}

